<template>
  <div>
    <div class='pds-tip'>
      <el-form :inline="true" class="filter">
        <el-form-item label="树类别:">
          <el-select v-model="docCategoryId" placeholder="请选择类别" @change="getDoctree">
            <el-option v-for="item in typeList" :key="item.docCategoryId" :label="item.name" :value="item.docCategoryId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <el-row>
      <el-col :span="7" style='margin-top:15px;margin-right:10px'>
        <el-card class="box-card">
          <div class="filter-container">
            <el-button-group>
              <el-button type="primary" v-if="menuManager_btn_add" icon="plus" @click="handlerAdd" :disabled="!docCategoryId">添加根节点</el-button>
              <!-- <el-button type="primary" v-if="menuManager_btn_edit" icon="edit" @click="handlerEdit" >编辑</el-button> -->
              <el-button type='primary' v-if="menuManager_btn_add" icon="plus" @click="handlerAddSon" :disabled="!(docCategoryId&&this.nodeInfo.id)">添加子节点
              </el-button>
            </el-button-group>
          </div>
          <el-tree class="filter-tree" :data="treeData" :props="defaultProps" default-expand-all highlight-current node-key="id" :filter-node-method="filterNode" @node-click="getNodeData" ref="treeData">
          </el-tree>
        </el-card>
      </el-col>
      <el-col :span="16" style='margin-top:15px;'>
        <el-card class="box-card">
          <el-form :label-position="labelPosition" :rules="rules" label-width="80px" :model="form" ref="form">
            <h3 v-if="formStatus == 'update'">文档类别编辑</h3>
            <h3 v-if="formStatus == 'create'">节点添加</h3>
            <el-form-item label="父类名称" v-if="form.parentName!=0">
              <!-- <span>{{form.parentId | parentId(form.parentId)}}</span> -->
              <span>{{form.parentName}}</span>
            </el-form-item>
            <el-form-item label="中文名称" prop="nameCn">
              <el-input v-model="form.nameCn" :disabled="formEdit" placeholder="请输入中文名称"></el-input>
            </el-form-item>
            <el-form-item label="英文名称" prop="nameEn">
              <el-input v-model="form.nameEn" :disabled="formEdit" placeholder="请输入英文名称"></el-input>
            </el-form-item>
            <el-form-item v-if="formStatus == 'create'">
              <el-button type="primary" @click="create">提交</el-button>
              <el-button @click="onCancel">取消</el-button>
            </el-form-item>
            <el-form-item v-if="formStatus == 'update'">
              <el-button type="primary" @click="update">更新</el-button>
              <el-button @click="onCancel">取消</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { mapGetters } from 'vuex';
import * as api from '@/api/product/category/index';
export default {
  data() {
    return {
      rules: {
        nameCn: {
          required: true,
          message: '请输入中文名称',
          trigger: 'blur'
        },
        nameEn: {
          required: true,
          message: '请输入英文名称',
          trigger: 'blur'
        }
      },
      typeList: [
        {
          docCategoryId: 1,
          name: '型号PGF'
        },
        {
          docCategoryId: 2,
          name: '型号QA Doc'
        },
        {
          docCategoryId: 3,
          name: '货号PGF'
        }
      ],
      filterText: '',
      list: null,
      total: null,
      formEdit: true,
      formAdd: true,
      formStatus: '',
      showElement: false,
      typeOptions: [],
      docCategoryId: '',
      treeData: [],
      nodeInfo: {},
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      labelPosition: 'right',
      form: {
        parentId: undefined,
        nameCn: undefined,
        nameEn: undefined,
        type: 'authority_menu_menu'
      },
      currentId: -1,
      menuManager_btn_add: false,
      menuManager_btn_edit: false,
      menuManager_btn_eadd: false,
      myname: ''
    };
  },
  watch: {
    filterText(val) {
      this.$refs.treeData.filter(val);
    }
  },
  created() {
    this.menuManager_btn_add = this.elements['menuManager:btn_add'];
    this.menuManager_btn_edit = this.elements['menuManager:btn_edit'];
  },
  computed: {
    ...mapGetters(['elements'])
  },
  mounted() {
    this.refresh()
  },
  methods: {
    refresh() {
      this.docCategoryId = null
      this.treeData = []
      this.form = {
        parentId: undefined,
        nameCn: undefined,
        nameEn: undefined,
        type: 'authority_menu_menu'
      }
      this.formEdit = true;
      this.formAdd = true
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 获取文档目录树数据
    getDoctree() {
      const params = { docCategoryId: this.docCategoryId };
      api.getDoctree(params).then(response => {
        this.treeData = response.data;
      });
    },
    getNodeData(val) {
      console.log('getNodeData', val)
      this.nodeInfo = val
      this.formEdit = false;
      // debugger
      if (this.form.docCategoryId) {
        this.formStatus = 'update';
      }
      const node = this.$refs.treeData.getCurrentNode();
      if (!this.formEdit) {
        this.formStatus = 'update';
      }
      // 点击某节点获取详情
      api.getDetail({ id: node.id }).then(response => {
        this.form = response.data;
        this.form.parentName = this.nodeInfo.nameCn
      });
    },
    // 添加根节点
    handlerAdd() {
      this.resetForm();
      this.formEdit = false;
      this.formStatus = 'create';
    },
    // 添加子节点
    handlerAddSon() {
      this.sonForm();
      // this.formEdit = false;
      this.formStatus = 'create';
    },
    // 修改
    update() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false;
        }
        api.updateDirectory(this.form).then(() => {
          this.getDoctree();
          this.$message({
            message: '更新成功',
            type: 'success',
            duration: 2000
          });
        });
      });
    },
    // 提交
    create() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false;
        }
        this.form.docCategoryId = this.docCategoryId;
        api.addDirectory(this.form).then(res => {
          this.getDoctree();
          if (res.code === 200) {
            this.$message({
              message: '创建成功',
              type: 'success'
            });
            return false;
          }
          this.$message({
            type: 'error',
            message: res.message
          });
        });
      });
    },
    onCancel() {
      this.formEdit = true;
      this.formStatus = '';
    },
    // 父节点添加传值
    resetForm() {
      this.form = {
        parentId: 0
      };
    },
    // 子节点添加传值
    sonForm() {
      const node = this.$refs.treeData.getCurrentNode();
      this.form = {
        parentId: node.id,
        cateLevel: ++node.cateLevel,
        parentName: this.nodeInfo.nameCn
      };
      this.formEdit = false
    }
  }
};
</script>
